package com.sz.biz.logistics.serialno.algorithm;

import com.sz.biz.logistics.serialno.algorithm.impl.RestrictedWeightArrayAlgorithm;
import com.sz.biz.logistics.serialno.algorithm.impl.WaybillNumberAlgorithm;
import com.sz.biz.logistics.serialno.dto.AlgorithmInfoDto;

import java.util.*;

import static com.sz.biz.logistics.serialno.constants.BillNumberConstants.AlgorithmInfo.RESTRICTED_WEIGHT_ARRAY_ALGORITHM;
import static com.sz.biz.logistics.serialno.constants.BillNumberConstants.AlgorithmInfo.WAYBILL_NUMBER_ALGORITHM;

/**
 * Package: com.sz.biz.logistics.channelno.algorithm<br>
 * Description: 算法工厂类<br>
 * @author WuHan<br>
 * @date 2017-12-07 17:39<br>
 * @serial
 * @since sys-1.1.3
 */
public class AlgorithmFactory {
    
    /**
     * 算法信息列表
     */
    public static List<AlgorithmInfoDto> algorithmInfoDtos =
            Arrays.asList(RestrictedWeightArrayAlgorithm.getAlgorithmInfoDto(),
                          WaybillNumberAlgorithm.getAlgorithmInfoDto());
    
    /**
     * 算法代号到算法类名的映射
     */
    public static Map<String,String> algorithmMap ;
    static {
        algorithmMap = new HashMap<>();
        algorithmMap.put(RESTRICTED_WEIGHT_ARRAY_ALGORITHM,RestrictedWeightArrayAlgorithm.class.getName());
        algorithmMap.put(WAYBILL_NUMBER_ALGORITHM, WaybillNumberAlgorithm.class.getName());
    }
    
    /**
     *
     * @return {@link #algorithmInfoDtos}
     * @param type
     */
    public static List<AlgorithmInfoDto> getAlgorithmInfoDtos(Integer type) {
        List<AlgorithmInfoDto> tempList = new ArrayList<>();
        algorithmInfoDtos.forEach(algorithmInfoDto -> {
            if (type.equals(algorithmInfoDto.getType())) {
                tempList.add(algorithmInfoDto);
            }
        });
        return tempList;
    }
    
    public static VerificationCodeAlgorithm getClass(String className) {
        VerificationCodeAlgorithm vca = null;
        
        try {
            vca = (VerificationCodeAlgorithm) Class.forName(className).newInstance();
        } catch (IllegalAccessException | InstantiationException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    
        return vca;
    }
}
